package sibots;

import static robocode.util.Utils.normalAbsoluteAngle;
import static robocode.util.Utils.normalRelativeAngle;
import jason.RoboJason;

import java.awt.Color;
import java.awt.Point;
import java.rmi.RemoteException;

import commons.Constants;

import robocode.HitWallEvent;
import robocode.rescue.ConstantesExecucao;
import robocode.rescue.RoboInfo;

public class RoboRefem extends RoboJason{
	
	private RoboInfo Target;
	private int xTeam;
	private int yTeam;
	
	public RoboRefem() { 
	}
	

	@Override
	public void mainLoop() {
		procuraTarget();
	}

	
	private void procuraTarget(){
		try {
			
			if ( Target != null){
				
				Target = referenciaServidor.getRoboInfo(team, Target.getNumRobo());
	
				double distancia = Point.distance(Target.getX(), Target.getY(), info.getX(), info.getY());
			
				if (  distancia >150){
					Target = null;
					referenciaServidor.setFollowing(team, -1);
				}
			}
			if ( Target == null){
				for ( RoboInfo ri : referenciaServidor.getTeamInfo(team)){
					
					double distance =  Point.distance(ri.getX(), ri.getY(), info.getX(), info.getY());
					System.out.println(ri.getNumRobo()+" ; "+ info.getNumRobo() + ":   "+ distance);
					if ( ri.getNumRobo() != info.getNumRobo() && distance <= 180){
						Target = ri;
						referenciaServidor.setFollowing(team, ri.getNumRobo());
					}
				}
			}
			if ( Target != null ){
				segueTarget();
				
			}else{
				System.out.println("doNothig");
				setVelocity(0);
				doNothing();
			}
			
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		execute();
	}
	private void segueTarget() {
		
		if(verificaSalvo()){
			System.out.println("SALVOOOOOOO");
			return;
		}
		double angle =  normalAbsoluteAngle(Math.atan2(Target.getX() - info.getX(), Target.getY() - info.getY()));
		double turn =  normalRelativeAngle(angle - getGunHeadingRadians())*180.0/Math.PI;
		System.out.println("Angulo:" + turn);
		System.out.println("Angulo:" + turn);
		if ( turn >= 0 && turn <= 180)setTurnRight(10);
		if ( turn < 0 && turn >= -180)setTurnLeft(10);
		
		//waitFor(new TurnCompleteCondition(this));
		setVelocity(Math.abs(Target.getVelocity()));
		if ( Point.distance(Target.getX(), Target.getY(), info.getX(), info.getY()) < 60){
			setVelocity(0);
		}
	}

	
	@Override
	public void onHitWall(HitWallEvent e) {
		setMaxVelocity(8);
    	double angulo = e.getBearing();
    	if (angulo > -90 && angulo <= 90) {
            setBack(10);
        } else {
            setAhead(10);
        }
    	
    	execute();
    	
	}
	private void setVelocity(double velocityRate) {
		
    	setMaxVelocity(velocityRate);
		
    	if (velocityRate > 0) {
    		setAhead(Double.POSITIVE_INFINITY);
	    } 
    	else if (velocityRate < 0) {
    		setBack(Double.POSITIVE_INFINITY);
		} 
    	else{
    		setAhead(0);
		}
    	
	}

	@Override
	public void setup() {

		if(Math.abs(Constants.LARGURA - info.getX()) > Constants.LARGURA /2 ){
			
			xTeam = 0;
			yTeam = Constants.ALTURA/2;
			
		}else{
			xTeam = Constants.LARGURA;
			yTeam = Constants.ALTURA/2;

		}
	}
	
	
	@Override
	public void setRobotColor() {
		if ( team.startsWith(ConstantesExecucao.nomeTeamA)){
			setColors(Color.WHITE, Color.BLUE, Color.WHITE);
		}
		if ( team.startsWith(ConstantesExecucao.nomeTeamB)){
			// TODO Auto-generated method stub
			setColors(Color.BLACK, Color.GREEN, Color.BLACK);
		}
	}

	
	public boolean verificaSalvo(){
		if(Math.abs(xTeam - info.getX()) > 100){
			 return false;
		}else{
			return true;
		}
	}
	
}

